package ch.zhaw.skuh.turing.controller;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.IOException;
import java.io.Writer;

import ch.zhaw.skuh.turing.model.turingmaschine.TuringChar;

public class LogWriter {

	// public class ReadWriteTextFile {

	/**
	 * Change the contents of text file in its entirety, overwriting any
	 * existing text.
	 * 
	 * This style of implementation throws all exceptions to the caller.
	 * 
	 * @param aFile
	 *            is an existing file which can be written to.
	 * @throws IllegalArgumentException
	 *             if param does not comply.
	 * @throws FileNotFoundException
	 *             if the file does not exist.
	 * @throws IOException
	 *             if problem encountered during write.
	 */
	public static void writeLog(File aFile, TuringChar tc, long time)
			throws FileNotFoundException, IOException {
		

		System.out.println("done: Calculation time [seconds] = " + Double.toString(time / Math.pow(10, 9)));
		System.out.println("Now preparing output ...");
		
		if (aFile == null) {
			throw new IllegalArgumentException("File should not be null.");
		}
		if (!aFile.exists()) {
			throw new FileNotFoundException("File does not exist: " + aFile);
		}
		if (!aFile.isFile()) {
			throw new IllegalArgumentException("Should not be a directory: "
					+ aFile);
		}
		if (!aFile.canWrite()) {
			throw new IllegalArgumentException("File cannot be written: "
					+ aFile);
		}

		// use buffering
		Writer output = new BufferedWriter(new FileWriter(aFile));
		try {
			// FileWriter always assumes default encoding is OK!
			
			output.write("Seconds:\n");
			output.write(Double.toString(time / Math.pow(10, 9)));
			output.write("\n");
			
			output.write("Steps:\n");
			output.write(Long.toString(tc.getMaschine().getSteps()));
			output.write("\n");
			
			output.write("State:\n");
			output.write(Integer.toString(tc.getMaschine().getState()));
			output.write("\n");
			
			output.write("Current Characters:\n");
			char[] tapesC = tc.getTapesAtCurrent();
			StringBuilder sb = new StringBuilder(tapesC.length);
			for(int iTape=0;iTape<tapesC.length;iTape++){
				sb.append('[');
				sb.append(tapesC[iTape]);
				sb.append(']');
			}
			output.write(sb.toString());
			output.write("\n");
			
			output.write("Current Positions:\n");
			int[] pos = tc.getPositions();			
			sb = new StringBuilder(pos.length);
			for(int iTape=0;iTape<pos.length;iTape++){
				sb.append('[');
				sb.append(Integer.toString(pos[iTape]));
				sb.append(']');
			}
			output.write(sb.toString());
			output.write("\n");
			
			output.write("Lengths of Tapes:\n");
			sb = new StringBuilder(tc.getTapeCount());
			StringBuilder[] tapes = new StringBuilder[tc.getTapeCount()];
			for(int i=0;i<tc.getTapeCount();i++){
				tapes[i] = new StringBuilder(tc.getTape(i));
				sb.append('[');
				sb.append(Integer.toString(tapes[i].length()));
				sb.append(']');
			}
			output.write(sb.toString());
			output.write("\n");

			output.write("\n");
			

			output.write("Full Tapes:\n");
			for(int i=0;i<tc.getTapeCount();i++){
				
				output.write(tapes[i].toString());

				output.write("\n");
				output.write("\n");
			}
		} finally {
			output.close();
		}
	}
}